package org.genntii.warehousesystem.controller;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.genntii.warehousesystem.common.Result;
import org.genntii.warehousesystem.domain.dto.UserAddressDTO;
import org.genntii.warehousesystem.domain.entity.UserAddress;
import org.genntii.warehousesystem.service.UserAddressService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户收货地址接口")
@RestController
@RequestMapping("/userAddress")
public class UserAddressController {

    @Resource
    private UserAddressService userAddressService;

    @Operation(summary = "添加收货地址")
    @PostMapping("/add")
    public Result add(HttpServletRequest request, @RequestBody UserAddressDTO dto){
        int userId = (int) request.getAttribute("id");
        userAddressService.add(userId,dto.getCityId(),dto.getAddress());
        return Result.success();
    }

    @Operation(summary = "删除收货地址")
    @Parameter(name = "id",description = "收货地址id",required = true,in = ParameterIn.QUERY)
    @DeleteMapping("del")
    public Result del(@RequestParam("id")int id){
        userAddressService.del(id);
        return Result.success();
    }

    @Operation(summary = "展示用户收货地址")
    @GetMapping("showAll")
    public Result<List<UserAddress>> showAll(HttpServletRequest request){
        int id = (int) request.getAttribute("id");
        return Result.success(userAddressService.showAll(id));
    }

    @Operation(summary = "修改收货地址信息")
    @Parameter(name = "id",description = "收货地址id",required = true,in = ParameterIn.QUERY)
    @PostMapping("update")
    public Result update(@RequestParam("id")int id, @RequestBody UserAddressDTO dto, HttpServletRequest request){
        int userId = (int) request.getAttribute("id");
        userAddressService.update(userId,dto.getCityId(),dto.getAddress(),id);
        return Result.success();
    }

}
